package com.example;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class JSQActivity extends AppCompatActivity implements View.OnClickListener {
    private EditText number1;
    private EditText number2;
    private TextView result;
    private EditText deal;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.jsqlayout);
        initView();
    }

    private void initView() {
        Button button=findViewById(R.id.bt_deal);
        button.setOnClickListener(this);
        number1=findViewById(R.id.et_num1);
        number2=findViewById(R.id.et_num2);
        result=findViewById(R.id.result);
        deal=findViewById(R.id.et_YSF);
    }

    @Override
    public void onClick(View view) {
        int id=view.getId();
        Float numer1=Float.parseFloat(number1.getText().toString());
        Float numer2=Float.parseFloat(number2.getText().toString());
        Float total;
        String fuhao = deal.getText().toString();
        switch (id){
            case R.id.bt_deal:
                switch (fuhao){
                    case "+":
                        total = numer1+numer2;
                        result.setText(total+"");
                        break;
                    case "-":
                        total = numer1-numer2;
                        result.setText(total+"");
                        break;
                    case "*":
                        total = numer1*numer2;
                        result.setText(total+"");
                        break;
                    case "/":
                        total = numer1/numer2;
                        result.setText(total+"");
                        break;
                    default:
                        Toast.makeText(this,"运算符输入错误，请重新输入",Toast.LENGTH_SHORT).show();
                        break;
                }

        }
    }
}
